﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Text;
using System.IO;

namespace RockCompany.Common.Lib
{
    public class Logger
    {
        public const string LOG_FILE_PATH = "\\Common\\Log\\app_log.log";
        static string logPath = Application.StartupPath + LOG_FILE_PATH;
        //static string logPath = Application.StartupPath + DBConnector.SOLUTION_DIR + LOG_FILE_PATH;
        const int LOG_MAX_SIZE = 5000000; //Max size of a log file
        public const string ERROR = "ERROR";

        public static void addLog(string sMessage) //Check log file exist and its size. Keep old log and create new log.
        {
            try
            {
                FileInfo fLog = new FileInfo(Path.GetFullPath(logPath));
                StreamWriter logWriter;
                if (!fLog.Exists)
                {
                    logWriter = fLog.CreateText();
                }
                else
                {
                    if (fLog.Length >= LOG_MAX_SIZE)
                    {
                        string newName = fLog.FullName + "." + DateTime.Now.ToString("yyyyMMddHHMMss");
                        fLog.CopyTo(newName);
                        logWriter = fLog.CreateText();
                    }
                    else
                    {
                        logWriter = fLog.AppendText();
                    }

                }
                logWriter.WriteLine(DateTime.Now.ToString() + " : ");
                logWriter.WriteLine(sMessage);
                logWriter.WriteLine();
                logWriter.Close();
            }
            catch (Exception ex)
            {
            }
        }
    }
}
